/*!
\copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.\n
            All Rights Reserved.\n
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\version    
\file       
\brief      Generate Event Bus Events
*/

#include "peer_status_channel_event_spec.h"
#include "event_bus_publish.h"

#include "tws_topology_private.h"
#include "tws_topology_peer_events.h"

#include "bt_device.h"

#include <logging.h>

static role_t twsTopology_ConvertRole(tws_topology_role role)
{
    return ((role == tws_topology_role_primary) ? primary_role : secondary_role); 
}

void TwsTopology_GeneratePeerPairingStartedEvent(bdaddr * peer_addr)
{
    peer_pairing_started_t data = { .peer_addr = *peer_addr};
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_pairing_started, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerPairingStartedEvent");
}

void TwsTopology_GeneratePeerPairingCompletedEvent(void)
{
    peer_pairing_complete_t data = { .peer_addr = {0}};
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_pairing_complete, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerPairingCompletedEvent");
}

void TwsTopology_GeneratePeerPairingFailedEvent(void)
{
    peer_pairing_failure_t data = { .peer_addr = {0}, .reason = reason_for_peer_pairing_failure_unknown };
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_pairing_failure, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerPairingFailedEvent");
}

void TwsTopology_GeneratePeerConnectingStartedEvent(void)
{
    peer_connecting_started_t data = { .peer_addr = {0}};
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_connecting_started, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerConnectingStartedEvent");
}

void TwsTopology_GeneratePeerAclConnectedEvent(void)
{
    peer_connecting_acl_connected_t data = { .peer_addr = {0}};
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_connecting_acl_connected, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerAclConnectedEvent");
}

void TwsTopology_GeneratePeerConnectedEvent(void)
{
    peer_connecting_complete_t data = { .peer_addr = {0}};
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_connecting_complete, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerConnectedEvent");
}

void TwsTopology_GeneratePeerConnectingFailedEvent(void)
{
    peer_connecting_failed_t data = { .peer_addr = {0}, .reason = reason_for_peer_connecting_failure_unknown };
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_connecting_failed, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerConnectingFailedEvent");
}

void TwsTopology_GeneratePeerDisconnectedEvent(void)
{
    peer_disconnected_t data = { .peer_addr = {0} };
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_disconnected, &data, sizeof(data));
    DEBUG_LOG_ALWAYS("TwsTopology_GeneratePeerDisconnectedEvent");
}

void TwsTopology_GeneratePeerRoleSwapInitiatedEvent(bool initiator)
{
    peer_role_swap_initiated_t data = { .peer_addr = {0}, 
                                        .current_role = initiator ? primary_role : secondary_role};
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_role_swap_initiated, &data, sizeof(data));
}

void TwsTopology_GeneratePeerRoleSwapCompleteEvent(tws_topology_role new_role_for_local_device)
{
    peer_role_swap_complete_t data = { .peer_addr = {0}, 
                                        .new_role = twsTopology_ConvertRole(new_role_for_local_device) };
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_role_swap_complete, &data, sizeof(data));
}

void TwsTopology_GeneratePeerRoleSwapTimeoutEvent(bool initiator)
{
    peer_role_swap_failed_t data = { .peer_addr = {0}, 
                                        .current_role = initiator ? primary_role : secondary_role, 
                                        .reason = reason_for_role_swap_fail_timeout };
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_role_swap_failed, &data, sizeof(data));
}

void TwsTopology_GeneratePeerRoleSwapVetoedEvent(void)
{
    peer_role_swap_failed_t data = { .peer_addr = {0}, 
                                        .current_role = primary_role, 
                                        .reason = reason_for_role_swap_fail_vetoed };
    appDeviceGetPeerBdAddr(&data.peer_addr);
    EventBus_Publish(EVENT_BUS_CHANNEL_PEER_STATUS, peer_role_swap_failed, &data, sizeof(data));
}